import { Component, OnInit } from '@angular/core';
import { Response } from '@angular/http';
import {SecondkillGoodsService} from '../secondkill-goods.service';
import {CartItem} from '../../../shared/common.interface';
import {CartService} from '../../../service/cart.service';
import {Cart1Service} from '../../../service/cart.service';
import {CartResult} from "../../../shared/cartResult.interface";

@Component({
  selector: 'app-seckill-detail',
  templateUrl: './seckill-detail.component.html',
  styleUrls: ['./seckill-detail.component.less']
})
export class SeckillDetailComponent implements OnInit {
  cartResult: CartResult ;
  seckillGoods;
  logined;
  product = {
    goodsId: 0,
    productIsSelected: false,
    userName: '',
    id: '',
    quantity: 0
  };
  constructor(private seckillGoodsService: SecondkillGoodsService,
              private cartService: Cart1Service) { }

  ngOnInit() {
    this.seckillGoodsService.getSeckillGoods().subscribe(
      (result) => {
        console.log(result);
        if (result.code === 1) {
          this.seckillGoods = result.data;
        }
      }
    );
  }
  addToCart(p: CartItem) {
   // this.logined = this.cartService.getToken();
   //  if (this.logined) {
   //    this.product['goodsId'] = p.goodsId;
   //    this.product.productIsSelected = true;
   //    this.product.userName = this.logined.data.userName;
   //    this.product.id = this.logined.data.userName + '_' + p.goodsId;
   //    this.product.quantity = 1;
   //    this.cartService.addToCart(this.product)
   //      .subscribe(
   //        (response) => {
   //          if (response.code === 1) {
   //            this.cartService.getCartList()
   //              .subscribe(
   //                (res) => {
   //                  this.cartResult = res;
   //                  this.cartResult.cart = res.cart;
   //                }
   //              );
   //          }
   //        }
   //      );
   //  }else {
   //    p.quantity = 1;
   //    this.cartService.localAddProduct(p);
   //  }
  }
}
